package com.easyidea.its.web.interceptor;

import java.util.Iterator;
import java.util.List;

import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.config.entities.ExceptionMappingConfig;
import com.opensymphony.xwork.interceptor.ExceptionHolder;

public class ExceptionMappingInterceptor extends com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor {

	private static final long serialVersionUID = 3137034978177545661L;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor#intercept(com.opensymphony.xwork.ActionInvocation)
	 */
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result;

        try {
            result = invocation.invoke();
        } catch (Exception e) {
            if (logEnabled) {
                handleLogging(e);
                e.printStackTrace();
            }
            List exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();
            String mappedResult = findResultFromExceptions(exceptionMappings, e);
            if (mappedResult != null) {
                result = mappedResult;
                publishException(invocation, new ExceptionHolder(e));
            } else {
                throw e;
            }
        }

        return result;
	}
    private String findResultFromExceptions(List exceptionMappings, Throwable t) {
        String result = null;

        // Check for specific exception mappings.
        if (exceptionMappings != null) {
            int deepest = Integer.MAX_VALUE;
            for (Iterator iter = exceptionMappings.iterator(); iter.hasNext();) {
                ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) iter.next();
                int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);
                if (depth >= 0 && depth < deepest) {
                    deepest = depth;
                    result = exceptionMappingConfig.getResult();
                }
            }
        }

        return result;
    }
}
